package org.jeeasy.system.modules.dict.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.jeeasy.common.core.domain.model.QueryPageModel;
import org.jeeasy.common.core.domain.vo.DictVo;
import org.jeeasy.common.core.enums.BooleanEnum;
import org.jeeasy.common.core.tools.QueryGenerator;
import org.jeeasy.system.modules.dict.domain.SysDict;
import org.jeeasy.system.modules.dict.domain.SysTableDict;
import org.jeeasy.system.modules.dict.mapper.SysDictMapper;
import org.jeeasy.system.modules.dict.service.SysDictService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author AlpsDDJ
 * @date 2020/11/9
 */
@Service
@Slf4j
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Override
    public List<SysDict> queryByParentId(String parentId) {
        return baseMapper.selectList(QueryGenerator.ofWrapper(SysDict.class).lambda().eq(SysDict::getParentId, parentId).orderByAsc(SysDict::getSortOrder));
    }

    @Override
    public List<SysDict> queryByParentCode(String parentCode) {
        return baseMapper.queryByParentCode(parentCode);
    }

    @Override
    public List<? extends DictVo> queryByTableDict(SysTableDict tableDict, boolean async) {
        String tableName = tableDict.getTableName();
        if (StrUtil.containsIgnoreCase(tableName, "select")) {
            tableDict.setTableName(StrUtil.concat(true, "(", tableName, ")"));
        }
        if (BooleanEnum.yes(tableDict.getIsTree())) {
            return async ? baseMapper.queryTreeByTableDictWithAsync(tableDict) : baseMapper.queryTreeByTableDict(tableDict);
        }

        return baseMapper.queryByTableDict(tableDict);
    }

    @Override
    public List<DictVo> getParent(SysTableDict tableDict, String value) {


        return null;
    }

    @Override
    public IPage<SysDict> queryPageTreeList(QueryPageModel query, HttpServletRequest req) {
        QueryWrapper<SysDict> wrapper = QueryGenerator.ofWrapper(SysDict.class, query);
        if (StrUtil.isEmpty(req.getParameter("parentId"))) {
            wrapper.lambda().eq(SysDict::getParentId, SysDict.TOP_ID);
        }
        wrapper.lambda().orderByAsc(SysDict::getSortOrder);
        IPage<SysDict> page = this.page(query.getPage(), wrapper);
        List<SysDict> list = page.getRecords();
        dictFilter(list);
        return page;
    }

    private void dictFilter(List<SysDict> list) {
        // 使用流式处理权限列表，过滤出非叶子节点
        list.forEach(perm -> {
            // 根据当前权限的id查询其所有子权限
            List<SysDict> children = queryByParentId(perm.getId());
            // 如果查询到子权限且不为空，则设置到当前权限的children属性中
            if (children != null && !children.isEmpty()) {
                perm.setChildren(children);
            }
        });
    }
}
